import {
    reqGetCodeImg,
    reqUserLogin,
    reqUserRegister,
    reqUserInfo,
    reqlogout,
    reqUpdateUserInfo,
    reqUpdateUserPassowrd
} from '@/api';
import {setToken, getToken, removeToken} from '../utils/token';

const state = {
    codeImgData: {},
    token: getToken(),
    userInfo: {},
};
const actions = {
    //验证码
    async getCodeImg({commit}) {
        let result = await reqGetCodeImg();
        // console.log('result :>> ', result);
        if (result.code == 200) {
            commit('GETCODEIMGDATA', result);
        }
    },
    //登录业务
    async reqUserLogin({commit}, user) {
        let result = await reqUserLogin(user);
        // console.log(result);
        //服务器下发token，用户唯一标识符(uuid)
        //将来经常通过带token找服务器要用户信息进行展示
        if (result.code === 200) {
            commit('USERLOGIN', result.token);
            setToken(result.token); //持久化存储token
            return result
        } else if (result.code === 500) {
            return result;
        }
    },
    //注册
    async userRegister({commit}, user) {
        let result = await reqUserRegister(user);
        // console.log(result);
        if (result.code == 200) {
            return result;
        } else {
            return result;
        }
    },
    //获取用户信息【需要带着用户的token向服务器要用户信息】
    async getUserInfo({commit}) {
        let result = await reqUserInfo();
        // console.log(result);
        if (result.code == 200) {
            commit('USERINFO', result.user);
            return 'ok';
        } else {
            return Promise.reject(new Error('fail'));
        }
    },
    // 退出登录
    async logout({commit}) {
        //只是向服务器发起一次请求，通知服务器清除token
        let result = await reqlogout();
        // console.log(result);
        //action里面不能操作state，提交mutation修改state
        if (result.code == 200) {
            commit('CLEAR', result.data);
        } else {
            return Promise.reject(new Error('falie'));
        }
    },
    // 修改个人信息
    async updateUserInfo({commit}, result) {
        let result1 = await reqUpdateUserInfo(result);
        // console.log(result);
        //action里面不能操作state，提交mutation修改state
        if (result1.code == 200) {
            return result1;
        } else {
            return result1;
        }
    },

    // 修改密码
    async updateUserPassowrd({commit}, password) {
        // console.log(password)
        let result = await reqUpdateUserPassowrd(password.oldPassword, password.newPassword);
        // console.log(result);
        //action里面不能操作state，提交mutation修改state
        if (result.code == 200) {
            return result;
        }else {
            return result;
        }
    },
};
const mutations = {
    GETCODEIMGDATA(state, codeImgData) {
        // console.log(codeImgData);
        state.codeImgData = codeImgData
    },
    USERLOGIN(state, token) {
        state.token = token;
    },
    USERINFO(state, userInfo) {
        state.userInfo = userInfo;
    },
    CLEAR(state) {
        //帮仓库中先关用户信息清空
        state.userInfo = {};
        state.token = '';
        //本地存储数据清空
        removeToken();
    },
};
const getters = {};
export default {
    namespaced: true,
    state,
    actions,
    mutations,
    getters,
};